home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / ttylink.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  3.5 KB  |  194 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <time.h>
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "socket.h"
  7. #include "telnet.h"
  8. #include "session.h"
  9. #include "proc.h"
  10. #include "tty.h"
  11. #include "mailbox.h"
  12. #include "commands.h"
  13. #include "cmdparse.h"
  14. #include "netuser.h"
  15.  
  16. static char Motd[85];
  17. int Attended = 1;
  18. static int Sttylink = -1;    /* Protoype socket for service */
  19. static int16 Bell = 2;
  20. static void near bell __ARGS((int16 val));
  21.  
  22. int
  23. dobell(argc,argv,p)
  24. int argc;
  25. char *argv[];
  26. void *p;
  27. {
  28.     return setintrc(&Bell,"Bell",argc,argv,0,10);
  29. }
  30.  
  31. /* if flag is set - restrict ax25, telnet and maybe other sessions */
  32. int
  33. doattended(argc,argv,p)
  34. int argc;
  35. char *argv[];
  36. void *p;
  37. {
  38.     return setbool(&Attended,"Attended",argc,argv);
  39. }
  40.  
  41. int
  42. domotd(argc,argv,p)
  43. int argc;
  44. char *argv[];
  45. void *p;
  46. {
  47.     int i;
  48.  
  49.     if(argc < 2 && Motd[0] != '\0') {
  50.         tputs(Motd);
  51.         return 0;
  52.     }
  53.     Motd[0] = '\0';
  54.  
  55.     for(i = 1; i < argc; i++) {
  56.         strcat(Motd,argv[i]);
  57.         if(strlen(Motd) > 80)
  58.             break;
  59.         strcat(Motd," ");
  60.     }
  61.     if(strlen(Motd) > 2)
  62.         strcat(Motd,"\n\0");
  63.     else
  64.         Motd[0] = '\0';
  65.     return 0;
  66. }
  67.  
  68. static void near
  69. bell(int16 val)
  70. {
  71.     while(val-- > 0) {
  72.         sound(320);
  73.         delay(160);
  74.         sound(480);
  75.         delay(160);
  76.         sound(640);
  77.         delay(160);
  78.         nosound();
  79.         pwait(NULL);
  80.     }
  81. }
  82.  
  83. int
  84. ttylstart(argc,argv,p)
  85. int argc;
  86. char *argv[];
  87. void *p;
  88. {
  89.     struct sockaddr_in lsocket;
  90.     int s, type;
  91.  
  92.     if(Sttylink != -1){
  93.         return 0;
  94.     }
  95.     psignal(Curproc,0);    /* Don't keep the parser waiting */
  96.     chname(Curproc,"TTYlink listener");
  97.  
  98.     lsocket.sin_family = AF_INET;
  99.     lsocket.sin_addr.s_addr = INADDR_ANY;
  100.     lsocket.sin_port = (argc < 2) ? IPPORT_TTYLINK : atoi(argv[1]);
  101.  
  102.     Sttylink = socket(AF_INET,SOCK_STREAM,0);
  103.     bind(Sttylink,(char *)&lsocket,sizeof(lsocket));
  104.     listen(Sttylink,1);
  105.     for(;;){
  106.         if((s = accept(Sttylink,NULLCHAR,(int *)NULL)) == -1)
  107.             break;    /* Service is shutting down */
  108.  
  109.         if(availmem() < Memthresh){
  110.             usputs(s,Nospace);
  111.             shutdown(s,1);
  112.         } else {
  113.             type = TELNET;
  114.             newproc("chat",1536,ttylhandle,s,(void *)&type,NULL,0);
  115.         }
  116.     }
  117.     return 0;
  118. }
  119.  
  120. /* This function handles all incoming "chat" sessions, be they TCP,
  121.  * NET/ROM or AX.25
  122.  */
  123. static void
  124. ttylhandle(s,t,p)
  125. int s;
  126. void *t;
  127. void *p;
  128. {
  129.     struct session *sp;
  130.     char addr[MAXSOCKSIZE];
  131.     struct telnet tn;
  132.     char *cp, *cp1;
  133.     int len = MAXSOCKSIZE, type = *(int *)t;
  134.  
  135.     sockmode(s,SOCK_ASCII);
  136.     sockowner(s,Curproc);    /* We own it now */
  137.  
  138.     /* Allocate a session descriptor */
  139.     if((sp = newsession(NULLCHAR,type,1,0)) == NULLSESSION){
  140.         usprintf(s,Nosess);
  141.         close_s(s);
  142.         return;
  143.     }
  144.  
  145.     log(s,"%4.4s open",Sestypes[type]);
  146.  
  147.     /* Initialize a Telnet protocol descriptor */
  148.     memset((char *)&tn,0,sizeof(tn));
  149.  
  150.     tn.session = sp;            /* Upward pointer */
  151.     sp->cb.telnet = &tn;        /* Downward pointer */
  152.     sp->s = s;
  153.     sp->proc = Curproc;
  154.  
  155.     getpeername(sp->s,addr,&len);
  156.     cp1 = strxdup(psocket(addr));
  157.  
  158.     if((cp = strchr(cp1,':')) != NULLCHAR)
  159.         *cp = '\0';
  160.  
  161.     tprintf("Incoming %s session from %s at %s",
  162.         Sestypes[type],cp1,ctime(&currtime));
  163.  
  164.     if(strcmp(cp1,inet_ntoa(Ip_addr)) == 0) {
  165.         xfree(cp1);
  166.         sp->name = strxdup("Chat");
  167.     } else {
  168.         sp->name = cp1;
  169.     }
  170.  
  171.     if(Attended && Bell)
  172.         bell(Bell);
  173.  
  174.     if(Motd != NULLCHAR)
  175.         usputs(s,Motd);
  176.  
  177.     usprintf(s,"\n%s - I'm %s...\n",Hostname,
  178.         Attended ? "here" : "absent. Pse leave msg");
  179.  
  180.     tnrecv(&tn);
  181. }
  182.  
  183. /* Shut down Ttylink server */
  184. int
  185. ttyl0(argc,argv,p)
  186. int argc;
  187. char *argv[];
  188. void *p;
  189. {
  190.     close_s(Sttylink);
  191.     Sttylink = -1;
  192.     return 0;
  193. }
  194.